|
Describes how Opbject oriented Programming can be applied to LotusScript
|
Object Oriented Programming is the dominant style in modern program as used by popular languages such as Java and C#. Despite this, OOP is not widely used within LotusScript even though the language support many of the capabilities needed to implement OOP. The following is an introduction to the key components of LotusScript used to develop using OOP. Classes - One of the key principals of Object Oriented Programming is the use of classes to encapuslate data objects.
- Once defined, a class should facilitate all access to the object via properties and the manipulation of that object via methods.
Class Names Class names must be unique across all LotusScript code that is references within a single module. This includes all "Use " statements and and "Use" statements contained in those LotusScripty libararies. Class names are not case sensitive. e.g. having defined a class Employee it is not possible to craete a second class callled employee and it is possible to refer to the class using EMPLOYEE.
Constructor - Each class can have a constructor denoted by Sub New
- When a class extends another class the constructors for each class are invoked starting with the based class and moving outwards.
- Unlike some programming languages, the constructor cannot be overloaded with differing number of parameters or different Types for each paremater. One way around this limitation is to provide a single parameter to the constructor of type Variant and then add logic to the constructor that tests the Type of the parameter.
Example Sub New(Source As Variant) Select Case Typename(Source) Case "NOTESDOCUMENT" Set iDocument = Source Case "NOTESUIDOCUMENT" Set iUIDocument = Source If (Source.Document Is Nothing) Then On Event PostOpen From Source Call OnPostOpen Else Set iDocument = Source.Document Case "NOTESVIEWENTRY" Set iDocument = Source.Document Case "STRING" Call GetByKey(Cstr(Source)) Case "STRING( )" Call GetByKey(Source) End Select End Sub
Class Destructor- Each class can have a destructor denoted by Sub Delete
- When a class extends another class the destructors for each class are invoked starting with the top class moving backwards. This is the opposite order to Constructors.
Class Extension- It is possible to extend a base class using syntax Class Newclass As Baseclass
- All Properties and Methods (including constructor and destructor) are carried forward from the base class.
- The new class can replace (override) a property or method defined in its base class. The new property/method must have the exact same signature for parameters as the underlying property/method.
- When a property/method is overriden, the property/method in the base class is not invoked (Exception: Constructor and Destructor)
- To invoke the method in a base class use the syntax Baseclass..method.
Properties- It is standard practice to expose public properties using the Property Get and Property Set statements.
- Whilst it is possible to define properties via Public variables, this is not favoured in traditional OOP.
Example '/** ' * Default form used to display document ' */ Property Get FormName As String If iDocument Is Nothing Then Exit Property If iDocument.HasItem("Form") Then FormName$ = iDocument.GetItemValue("Form")(0) End Property Property Set FormName As String If iDocument Is Nothing Then Exit Property Call iDocument.ReplaceItemValue("Form",FormName$) End Property
Events - It is possible to trap events from Notes UI classes in any class that extends these classes using the Syntax On Event eventName From Notesclass Call eventhandlername
- It is recommended that event handler be assigned a easily identifiable name using a convenient naming standard such as OnEventname
Example On Event PostOpen from NotesDocument Call OnPostOpen
|